package com.ews.onlineexchange.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.ConversationScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import org.apache.commons.collections.CollectionUtils;

import com.ews.onlineexchange.model.AgentService;
import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.QualificationGroup;
import com.ews.onlineexchange.model.QualificationTemplate;
import com.ews.onlineexchange.model.Relation;
import com.ews.onlineexchange.model.RequireType;
import com.ews.onlineexchange.model.ServiceSupplier;

@Named
@ConversationScoped
public class CandidateSupplierSelector implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	SupplierSelector supplierSelector;
	
	@Inject
	ServiceSupplierAgentServiceSelectorList agentServiceSelectorList;

	public List<ServiceSupplier> getResultList(Bulletin bulletin) {
		QualificationGroup rootGroup = bulletin.getQualificationGroup();
		supplierSelector.setMaxResults(null);
		Collection<ServiceSupplier> resultList = new ArrayList<>();
			for (QualificationGroup group : rootGroup.getGroups()) {
				Collection<ServiceSupplier> groupResult = new ArrayList<>();
				
				if(group.getRequireType().equals(RequireType.QualificationRequirement)) {
					groupResult = getResultOfSingleGroup(group);
				}else {
					groupResult = getResultOfSingleGroup_agentService(group);
				}
				
				if (rootGroup.getRelation().equals(Relation.Or)) {//最外层关系判断
					resultList = CollectionUtils.union(resultList, groupResult);
				} else {
					if (resultList.isEmpty()) {
						resultList = groupResult;
					} else {
						resultList = CollectionUtils.retainAll(resultList, groupResult);
					}
						
				}
			}
//		else {//服务事项要求选取
//			
//			for (QualificationGroup group : rootGroup.getGroups()) {
//				Collection<ServiceSupplier> groupResult = getResultOfSingleGroup_agentService(group);
//				if (rootGroup.getRelation().equals(Relation.Or)) {
//					resultList = CollectionUtils.union(resultList, groupResult);
//				} else {
//					if (resultList.isEmpty()) {
//						resultList = groupResult;
//					} else 
//						resultList = CollectionUtils.retainAll(resultList, groupResult);
//				}
//			}
//		}
		
		List<ServiceSupplier> suppliers = new ArrayList<ServiceSupplier>(new HashSet<ServiceSupplier>(resultList));
		for(int i = 0; i < suppliers.size() ; i++) {
			ServiceSupplier serviceSupplier = suppliers.get(i);
			/*
			 * 分数不够的中介，不能接受邀请
			 */
			if ( serviceSupplier.getCreditScore().compareTo( bulletin.getCreditScoreLimit() ) < 0 ) {
				suppliers.remove(i);
				i--;
			}
		}
		return suppliers;
	}
	
	private Collection<ServiceSupplier> getResultOfSingleGroup(QualificationGroup group) {
		Collection<ServiceSupplier> resultList = new ArrayList<>();
		for (QualificationTemplate _template : group.getQualificationTemplates()) {
//			supplierSelector.setQualificationTemplate(_template);
			List<ServiceSupplier> temResultList = supplierSelector.getSupplierByQualificationTemplate(_template);
			if (group.getRelation().equals(Relation.Or)) {
				resultList = CollectionUtils.union(resultList, temResultList);
			} else {
				if (resultList.isEmpty()) {
					resultList = temResultList;
				} else 
					resultList = CollectionUtils.retainAll(resultList, temResultList);
			}
		}
		return resultList;
	}
	
	private Collection<ServiceSupplier> getResultOfSingleGroup_agentService(QualificationGroup group) {
		Collection<ServiceSupplier> resultList = new ArrayList<>();
		for (AgentService service : group.getAgentServices()) {
//			supplierSelector.setQualificationTemplate(_template);
			agentServiceSelectorList.setAgentService(service);
			List<ServiceSupplier> temResultList = agentServiceSelectorList.getResultList();
			if (group.getRelation().equals(Relation.Or)) {
				resultList = CollectionUtils.union(resultList, temResultList);
			} else {
				if (resultList.isEmpty()) {
					resultList = temResultList;
				} else 
					resultList = CollectionUtils.retainAll(resultList, temResultList);
			}
		}
		return resultList;
	}


	
	
	
}
